//
//  UIImageView+YHWebImage.m
//  YHKit
//
//  Created by Mac on 2021/4/15.
//

#import "UIImageView+YHWebImage.h"
#import <objc/message.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import <YHConstant/YHConstant.h>
#import "UIImage+YHStyle.h"
#import "NSString+YH.h"
#import "NSString+YHWebImage.h"
#import "YHConstant+Null.h"

@implementation UIImageView (YHWebImage)

-(void)yh_setImage:(NSString *)urlString{
    [self yh_setImage:urlString placeholderImage:[UIImage yh_placeHolder]];
}

-(void)yh_setImageOrigin:(NSString *)urlString{
    [self yh_setImage:urlString placeholderImage:[UIImage yh_placeHolder] isOrigin:YES];
}

-(void)yh_setImage:(NSString *)urlString placeholderImage:(nullable UIImage *)placeholderImage{
    [self yh_setImage:urlString placeholderImage:placeholderImage isOrigin:NO];
}

-(void)yh_setImage:(NSString *)urlString placeholderImage:(nullable UIImage *)placeholderImage isOrigin:(BOOL)isOrigin{
    
    if (IsNull(placeholderImage)) {
        placeholderImage = [UIImage yh_placeHolder];
    }
    if(![urlString isKindOfClass:[NSString class]]){
        self.image = placeholderImage;
        return;
    }
    NSURL * url = [NSURL URLWithString:urlString];
    if ([url pathComponents].count > 1) {
        NSString * imageURL = urlString.yh_OSSURLPath;
        if(!isOrigin){
            imageURL = [imageURL yh_OSSReszeFit];
        }
        [self sd_setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:placeholderImage];
        
    }else if (IsNull(urlString)) {
        self.image = placeholderImage;
    }else{
        self.image = [UIImage imageNamed:urlString];
        if (IsNull(self.image)) {
            self.image = placeholderImage;
        }
    }
}





@end
